package com.waste.game.controller;

import com.waste.game.MyAnnotation.UserLoginToken;
import com.waste.game.MyAnnotation.PassToken;
import com.waste.game.dao.VideoUserDao;
import com.waste.game.entity.entitiytv.VideoUserBean;
import com.waste.game.service.TokenService;
import com.waste.game.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
public class VideoLoginController {

    @Autowired
    private VideoUserDao videoUserDao;
    @Autowired
    TokenService tokenService;
    //用户注册
//    @PostMapping("/api/register")
//    public int addUser(@RequestBody VideoUserBean videoUserBean){
//        return videoUserDao.addUser(videoUserBean);
//    }
    //用户登录
    @PostMapping("/api/login")
    public Object login(@RequestBody VideoUserBean videoUserBean){
        JSONObject jsonObject = new JSONObject();
        VideoUserBean user = videoUserDao.getUser(videoUserBean.getUserName());
        boolean falg = false;
        if (user != null){
            if (user.getUserPassWord().equals(videoUserBean.getUserPassWord())){
                String token = tokenService.getToken(user);
                jsonObject.put("token",token);
                jsonObject.put("userId",user.getUserName());
                jsonObject.put("message","登陆成功");
                return jsonObject.toString();
            }else {
                jsonObject.put("message","登陆失败");
                return jsonObject.toString();
            }
        }else {
            jsonObject.put("message","没有此用户");
            return jsonObject.toString();
        }
    }
    @UserLoginToken
    @GetMapping("/admin/getMessage")
    public String getMessage() {

        // 取出token中带的用户id 进行操作
        System.out.println(TokenUtils.getTokenUserId());

        return ""+TokenUtils.getTokenUserId()+"你已通过验证";
    }
}
